package main

import (
	"fmt"
	"io"
	"net"
	"os"
)

func main() {
	conn, err := net.Dial("tcp", "baidu.com:80")
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
		return
	}
	defer conn.Close()

	reqStr := "HEAD / HTTP/1.1\r\n" + // HEAD请求，只返回请求头
		"Host: baidu.com\r\n" + 
		"Connection: close\r\n" + // 返回后，服务器会断开连接，默认是keep-alive
		"\r\n"  // 请求头结束
	_, err = io.WriteString(conn, reqStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
		return
	}

	buf := make([]byte, 1024)
	for {
		n, err := conn.Read(buf)
		fmt.Println(string(buf[:n]))
		if err != nil {
			if err == io.EOF {
				fmt.Println("END")
				break
			} else {
				fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
			}
		}
	}
}
